﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using EyFramework.Common;
using EyFramework.Library;
using ShuJuGj.Core.Dao;
using ShuJuGj.Core.Service;
using ShuJuGj.Entity;

namespace ShuJuGj
{
    public partial class FrmShuJuGj : FrmDevBase
    {
        private ISjgjShujujiService sjgjShujujiService;
        public String Seq;

        public FrmShuJuGj()
        {
            InitializeComponent();
        }

        private void FrmShuJuGj_Load(object sender, EventArgs e)
        {
            sjgjShujujiService = Globals.GetService<ISjgjShujujiService>();
            InitForm();
            QueryData();
        }

        public void QueryData()
        {
            gcData.DataSource = ChangeData(sjgjShujujiService.GetListBy());
            DevPublic.SetBindGridControlView(gvData, true, true);
        }

        private void InitForm()
        {
            gvData.ShowRowNum();
            gcData.ShowContextMenu(this.components);//显示右键导出功能
            DevExpress.XtraGrid.GridFormatRule gfrId = new DevExpress.XtraGrid.GridFormatRule() { Name = "gfrId" };
            // 指定应用到编号列
            gfrId.Column = gvData.Columns["FLAG"];
            gfrId.Rule = new DevExpress.XtraEditors.FormatConditionRuleExpression()
            {
                // 使用Dev默认提供的预定义样式
                PredefinedName = "Red Fill, Red Text",
                Expression = "[FLAG]='N' ", // 业务状态为N时报红
            };
            // 是否应用到整行
            gfrId.ApplyToRow = true;
            this.gvData.FormatRules.Add(gfrId);
        }

        private void RitsEdit_Toggled(object sender, EventArgs e)
        {
            SjgjShujuji sjgjShujuji = gvData.GetRow(gvData.FocusedRowHandle) as SjgjShujuji;
            sjgjShujuji.FLAG = sjgjShujuji.FLAG == "Y" ? "N" : "Y";
            if (sjgjShujuji.FLAG == "N")
            {
                //改成“N”时出现弹窗
                if (Globals.ShowYesNoAndWarning("是否停用当前数据集？") != DialogResult.Yes)
                {
                    QueryData();
                    return;
                }
            }
            sjgjShujujiService.updateStates(sjgjShujuji);
            QueryData();
        }

        private List<SjgjShujuji> ChangeData(List<SjgjShujuji> sjgjShujujis)
        {
            List<SjgjShujuji> sjgjs = sjgjShujujis;
            for (int i = 0; i < sjgjShujujis.Count; i++)
            {
                if (sjgjShujujis[i].CONNECT_TYPE == "1")
                {
                    sjgjs[i].CONNECT_TYPE_NM = "his数据库";
                }
                else if (sjgjShujujis[i].CONNECT_TYPE == "2")
                {
                    sjgjs[i].CONNECT_TYPE_NM = "体检数据库";
                }

                if (sjgjShujujis[i].TASK_TYPE == "1")
                {
                    sjgjs[i].TASK_TYPE = "增量上传";
                }
                else if (sjgjShujujis[i].TASK_TYPE == "2")
                {
                    sjgjs[i].TASK_TYPE = "实时上传";
                }
            }
            return sjgjs;
        }

        private async void repositoryItemButtonEdit1_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            ShowWaiting();
            try
            {
                await Task.Run(() =>
                {
                    SjgjShujuji sjgjShujuji = gvData.GetRow(gvData.FocusedRowHandle) as SjgjShujuji;
                    UploadService uploadService = new UploadService();
                    uploadService.showLoadingText = showLoadingText;
                    uploadService.UploadDataSet(sjgjShujuji);
                    //if (sjgjShujuji.TBNAME.ToUpper() == "SJGJ_JKTJBGJL")
                    //{
                    //    uploadService.TjbgPdfUpload();
                    //}
                });
            }
            finally
            {
                CloseWaiting();
            }
        }

        public void showLoadingText(string text)
        {
            ShowWaiting(text);
        }
    }
}